package org.nimi317.web_gis.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.nimi317.web_gis.Enum.ProvinceDataEnum;
import org.nimi317.web_gis.data.AreaCodeList;
import org.nimi317.web_gis.form.post.ProvinceRenderPost;
import org.nimi317.web_gis.model.AirQualityPredict;
import org.nimi317.web_gis.entity.AreaQuality;
import org.nimi317.web_gis.entity.R;
import org.nimi317.web_gis.service.AreaService;
import org.nimi317.web_gis.service.IAreaQualityService;
import org.nimi317.web_gis.service.IAreaWeatherService;
import org.nimi317.web_gis.utils.TimeUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * @author thunderobot
 */
@RestController
@RequestMapping("areas")
@SaCheckLogin
public class AreaController {

    private final AreaService areaService;

    private final IAreaQualityService areaQualityService;

    private final IAreaWeatherService areaWeatherService;

    private final AirQualityPredict predict;

    private final AreaCodeList codeList;

    public AreaController(AreaService areaService, IAreaQualityService areaQualityService, IAreaWeatherService areaWeatherService, AirQualityPredict predict, AreaCodeList codeList) {
        this.areaService = areaService;
        this.areaQualityService = areaQualityService;
        this.areaWeatherService = areaWeatherService;
        this.predict = predict;
        this.codeList = codeList;
    }

    @GetMapping
    public R<?> getAreas() {
        return R.ok(codeList.getAreaCodes());
    }

    @PostMapping("history")
    public R<?> getAreaHistory(@RequestBody @Validated ProvinceRenderPost post) {
        //获取当前市区历史数据
        LambdaQueryWrapper<AreaQuality> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AreaQuality::getCityCode, post.getId())
                .ge(AreaQuality::getTimestamp, TimeUtils.getDivisionDateTime(post.getType()));
        List<AreaQuality> list = areaQualityService.list(wrapper);
        return getR(list);
    }

    @GetMapping("health/{id}")
    @Deprecated
    public R<?> getHealth(@PathVariable String id) {
        //获取当前市区历史数据
        LambdaQueryWrapper<AreaQuality> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AreaQuality::getCityCode, id)
                .ge(AreaQuality::getTimestamp, TimeUtils.getDivisionDateTime(ProvinceDataEnum.Month));
        List<AreaQuality> list = areaQualityService.list(wrapper);
        return getR(list);
    }

    private R<?> getR(List<AreaQuality> list) {
        return R.ok(Map.of("quality", list));
    }


    @GetMapping("predict/{id}")
    private R<?> getPredict(@PathVariable String id) {
        List<AreaQuality> recentAreaQuality = areaQualityService.getRecentAreaQuality(id);
        List<AirQualityPredict.Data> predictData = predict.predictData(recentAreaQuality.stream().sorted(Comparator.comparing(AreaQuality::getTimestamp)).toList());
        return R.ok(predictData);
    }

    @GetMapping("realTime")
    private R<?> getRealTime() {
        // 获取市区数据 然后查询
        return R.ok(areaQualityService.getRealTime());
    }
}
